﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Brain.Editor
{
    public class DefaultEditorManager : IEditorManager
    {
        public void DeserializedAll()
        {
        }
        /// <summary>
        /// The Database of your game
        /// </summary>
        public DatabaseManager DatabaseManager { get; set; }

        /// <summary>
        /// The Plugin Manager, responsible for
        /// handling Plugins DLLs
        /// </summary>
        public IEditorPluginManager PluginManager { get; set; }

        private object selectedObject;
        public object SelectedObject
        {
            get { return selectedObject; }
            set
            {
                selectedObject = value;

                if (OnSelectedNewObject != null)
                {
                    OnSelectedNewObject(value);
                }
            }
        }

        /// <summary>
        /// The selected sprite
        /// </summary>
        private ISprite selectedSprite;
        public ISprite SelectedSprite
        {
            get { return selectedSprite; }
            set
            {
                selectedSprite = value;
                SelectedObject = value;
            }
        }
        /// <summary>
        /// The selected Picked Object
        /// </summary>
        private IPickedObject selectedWorldObject;
        public IPickedObject SelectedWorldObject
        {
            get { return selectedWorldObject; }
            set
            {
                selectedWorldObject = value;
                SelectedObject = value;
            }
        }


        public event SetObject OnSelectedNewObject;

        public DefaultEditorManager()
        {
            DatabaseManager = new DatabaseManager();

            PluginManager = new DefaultEditorPluginManager();
        }

        /// <summary>
        /// Event called when a XNA View Control is drawed
        /// </summary>
        public void OnDraw(IPickingManager2D pick2D, IPickingManager3D pick3D)
        {
            if (pick2D != null)
            {
                ISprite sel = pick2D.SelectedSprite;
                if (SelectedSprite != sel)
                {
                    SelectedSprite = sel;
                    if (SelectedSprite != null)
                    {
                        Engine.Instance.Editor.MakeInterfaceHolder(selectedSprite);
                    }
                }
            }

            if (pick3D != null)
            {
                IPickedObject wor = pick3D.SelectedWorldObject;
                if (SelectedWorldObject != wor)
                {
                    SelectedWorldObject = wor;
                    if (SelectedWorldObject != null)
                    {
                        Engine.Instance.Editor.MakeInterfaceHolder(selectedWorldObject);
                    }
                }
            }
        }

        public void NewLevel()
        {
            this.selectedObject = null;
            this.selectedSprite = null;
            this.selectedWorldObject = null;

            PluginManager = new DefaultEditorPluginManager();

        }

        public void FinishedInsertingTypes()
        {
            DatabaseManager.FinishedInsertingTypes();
        }

        public void Reset()
        {
            this.selectedObject = null;
            this.selectedSprite = null;
            this.selectedWorldObject = null;

        }

        public SerializationData Data { get; set; }

        public void SerializeData(DefaultSerializationManager manager)
        {
        }

        public void DeserializeData(DefaultSerializationManager manager, SerializationData newData)
        {
        }

    }
}
